const express = require('express');
const router = express.Router();
const verify = require('./verify');
const dtoUser = require('./dto/user');
const tool = require('../utils/tools');
const db = require('../dataBase/db');

router.get('/getId', async(req, res) => {
    let dtoRes = await dtoUser.genUserId();
    res.send(dtoRes);
})

router.post('/login', async(req, res) => {
    let userId = req.body.userId ? req.body.userId : "";
    let password = req.body.password ? req.body.password : "";

    if (userId == "" || password == "") {
        res.send({ status: false, content: "参数不合法" });
        return;
    }

    let dbRes = await dtoUser.userLogin(userId, password);
    res.send(dbRes);

})

router.post('/register', async(req, res) => {
    let userId = req.body.userId ? req.body.userId : "";
    let password = req.body.password ? req.body.password : "";
    let code = req.body.code ? req.body.code : "";

    if (code != "NZY9") {
        res.send({ status: false, content: "code不正确!" });
        return;
    }
    if (userId == "" || password == "" || code == "") {
        res.send({ status: false, content: "参数不合法" });
        return;
    }

    let dbRes = await dtoUser.userReg(userId, password);
    res.send(dbRes);
})

router.use(async(req, res, next) => {
    let token = req.body.token;
    let tokenRes = await verify(token);
    if (tokenRes.status) {
        req.userData = tokenRes.userData;
        next();
    } else {
        res.send(tokenRes);
    }
});

router.post('/verify', async(req, res) => {
    res.send({ status: true, content: 'token认证成功!' });
})

router.post('/getUserInfo', async(req, res) => {
    let userInfo = {};
    userInfo.name = req.userData.name;
    userInfo.avatarImg = req.userData.avatarImg;
    res.send({ status: true, content: "获取个人信息成功", data: userInfo });
})

router.post('/test', async(req, res) => {
    console.log(req.userData);
    res.send('success');
})

router.post('/createChat', async(req, res) => {
    let userId = req.userData.userId;
    let name = req.body.name ? req.body.name : "";
    let secondKey = req.body.secondKey ? req.body.secondKey : "";
    let amount = parseInt(req.body.amount) != NaN && parseInt(req.body.amount) >= 2 ? parseInt(req.body.amount) : 2;

    if (name == "" || secondKey == "") {
        res.send({ status: false, content: "参数不合法" });
        return;
    }

    let dtoRes = await dtoUser.createChat(userId, name, secondKey, amount);
    res.send(dtoRes);
})


router.post('/joinChat', async(req, res) => {
    let userId = req.userData.userId;
    let chatId = req.body.chatId ? req.body.chatId : "";
    let secondKey = req.body.secondKey ? req.body.secondKey : "";

    if (chatId == "" || secondKey == "") {
        res.send({ status: false, content: "参数不合法" });
        return;
    }

    let dtoRes = await dtoUser.joinChat(userId, chatId, secondKey);
    res.send(dtoRes);
});

router.post('/getChatList', async(req, res) => {
    let dtoRes = await dtoUser.getChatList(req.userData);
    res.send(dtoRes);
});

router.post('/getChatListManage', async(req, res) => {
    let dtoRes = await dtoUser.getChatListManage(req.userData);
    res.send(dtoRes);
});

router.post('/getChatDetails', async(req, res) => {
    let chatId = req.body.chatId ? req.body.chatId : "";
    if (chatId == "") {
        res.send({ status: false, content: "参数不合法" });
        return;
    }
    let dtoRes = await dtoUser.getChatDetail(req.userData, chatId);
    res.send(dtoRes);
});

router.post('/verifySecondKey', async(req, res) => {
    let chatId = req.body.chatId ? req.body.chatId : "";
    let secondKey = req.body.secondKey ? req.body.secondKey : "";
    if (chatId == "" || secondKey == "") {
        res.send({ status: false, content: "参数不合法" });
        return;
    }

    let dbRes = await db.Chats.findOne({ id: chatId });
    if (dbRes && dbRes.secondKey == secondKey) {
        res.send({ status: true, content: "密钥合法" });
    } else {
        res.send({ status: false, content: "密钥非法" });
    }
});

router.post('/destoryChat', async(req, res) => {

});

router.post('/updateChatTime', async(req, res) => {
    let date = tool.getNowDate();
    let chatId = req.body.chatId ? req.body.chatId : "";
    if (chatId == "") {
        res.send({ status: false, content: "参数不合法" });
        return;
    }
    try {
        let dbRes = await db.Users.updateOne({ userId: req.userData.userId, "chatList.id": chatId }, { $set: { "chatList.$.latestTime": date } });
        res.send({ status: true, content: "更新时间成功", date: date });
    } catch (err) {
        console.log(err);
        res.send({ status: false, content: "更新失败" });
    }
});

module.exports = router;